﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using  System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;
using Agnes.Core;
using DevExpress.Xpf.Editors;
using DevExpress.Xpf.Scheduler;
using MessageBox = Microsoft.Windows.Controls.MessageBox;

namespace Agnes.TaskManager
{
    /// <summary>
    /// 标签编辑模型
    /// </summary>
    public class TaskLabelEditViewModel 
        : ScreenBase<ObservableCollection<AppointmentLabel>>
    {
        private AppointmentLabel _selLabel { get; set; }
        /// <summary>
        /// 选中的标签
        /// </summary>
        public AppointmentLabel SelLabel
        {
            get { return _selLabel; }
            set
            {
                _selLabel = value;
                if (_selLabel == null)
                {
                    CanModify = false;
                    CanDel = false;
                }
                else
                {
                    LabelDisplayName = SelLabel.DisplayName;
                    LabelColor = SelLabel.Color;
                    CanModify = true;
                    CanDel = true;
                }
                
                NotifyOfPropertyChange(() => SelLabel);
            }
        }

        private string _labelDisplayName;
        /// <summary>
        /// 标签的显示名称
        /// </summary>
        public string LabelDisplayName
        {
            get { return _labelDisplayName; }
            set
            {
                _labelDisplayName = value;
                NotifyOfPropertyChange(() => LabelDisplayName);
            }
        }

        private Color _labelColor;
        /// <summary>
        /// 新建修改标签的颜色
        /// </summary>
        public Color LabelColor
        {
            get { return _labelColor; }
            set
            {
                _labelColor = value;
                NotifyOfPropertyChange(() => LabelColor);
            }
        }

        public TaskLabelEditViewModel()
        {
            Model = TaskManagerService.TaskManager.SchedulerStorage.AppointmentStorage.Labels;
            DisplayName = "任务标签编辑";
            
        }

        protected override object CreateView()
        {
            return new TaskLabelsEditView();
        }

        public void Add()
        {
            if (LabelDisplayName == "")
            {
                MessageBox.Show("标签名不能为空", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            foreach (AppointmentLabel i in Model)
            {
                if (i.DisplayName == LabelDisplayName)
                {
                    MessageBox.Show("相同名字的标签已经存在", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            AppointmentLabel appointmentLabel = new AppointmentLabel(LabelColor, LabelDisplayName);
            Model.Add(appointmentLabel);

            TaskManagerService.TaskManager.SaveLabels();
        }

        public bool _canModify = false;
        public bool CanModify
        {
            get { return _canModify; }
            set
            {
                _canModify = value;
                NotifyOfPropertyChange(() => CanModify);
            }
        }

        public void Modify()
        {
            SelLabel.Color = LabelColor;
            SelLabel.DisplayName = LabelDisplayName;
            Model = null;
            Model = TaskManagerService.TaskManager.SchedulerStorage.AppointmentStorage.Labels;
            TaskManagerService.TaskManager.SaveLabels();
        }

        public bool _canDel = false;
        public bool CanDel
        {
            get { return _canDel; }
            set
            {
                _canDel = value;
                NotifyOfPropertyChange(() => CanDel);
            }
        }

        public void Del()
        {
            Model.Remove(SelLabel);
            TaskManagerService.TaskManager.SaveLabels();
        }
    }
}
